<template>
	<view>
		<view class="list">
			<view class="list-one" v-for="(item, list) in consultlist" :key="list">
				<view class="right">
					<view class="bottm">
						<u-parse :content="item.info"></u-parse>
					</view>
					<view class="time">{{ item.createTime }}</view>
					<view class="time" style="display: flex">
						<b>联系人：</b>
						{{ item.contactNickname }}
					</view>
					<view class="time" style="font-weight: bold; padding: 0 0; display: flex" @click="call_phone(item.contactMobile)">
						联系电话：
						<u--text :text="item.contactMobile" color="#8BB950" bold></u--text>
					</view>
				</view>
				<view class="left">
					<block v-for="(file, img) in item.pics" :key="img">
						<u--image
							width="315rpx"
							height="315rpx"
							:src="file"
							radius="4"
							v-if="getMediaType(file) == 'image'"
							style="margin: 0 8rpx 20rpx; float: left"
							@click="click_listone(file)"
						>
							<template v-slot:loading>
								<u-loading-icon color="#39488a"></u-loading-icon>
							</template>
						</u--image>
						<video
							:src="file"
							controls
							v-if="getMediaType(file) == 'video'"
							style="width: 100%; height: 320rpx; float: left; margin-bottom: 20rpx; border-radius: 8rpx"
						></video>
					</block>
					<view class="clear"></view>
				</view>
			</view>
		</view>
		<u-empty :show="consultlist.length == 0 && loading ? true : false" marginTop="100"></u-empty>
		<u-loadmore :status="loadstatus" line v-if="consultlist.length > 0" />
		<view style="height: 140rpx"></view>
		<view class="btn" @click="show = true">
			<view class="topval">发布</view>
			<view class="bottmval">随手拍</view>
		</view>
		<u-popup :show="show" @close="close" :round="10" closeable>
			<view class="popup-tit">发布随手拍</view>
			<view class="popup-cont">
				<!-- <view class="tit">发布标题</view>
				<view class="text">
					<u--input v-model="formdata.title" placeholder="请输入发布标题"></u--input>
				</view> -->
				<view class="tit">发布内容</view>
				<view class="text">
					<u--textarea v-model="formdata.info" placeholder="请输入内容" count></u--textarea>
				</view>
				<view class="tit">联系人</view>
				<view class="text">
					<u--input v-model="formdata.contactNickname" placeholder="请输入联系人"></u--input>
				</view>
				<view class="tit">联系电话</view>
				<view class="text">
					<u--input v-model="formdata.contactMobile" placeholder="请输入联系电话"></u--input>
				</view>
				<view class="tit">照片或视频</view>
				<u-upload
					:fileList="fileList2"
					@afterRead="afterRead"
					@delete="deletePic"
					:previewFullImage="false"
					name="2"
					:maxCount="9"
					accept="file"
					uploadText="照片或视频"
					:sizeType="['compressed']"
				></u-upload>
				<view style="color: red; font-size: 24rpx" v-if="errtext">{{ errtext }}</view>
				<view class="btns" @click="click_idea">发布</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
import { ssppage, sspcreate } from '@/api/index.js';
import { commonUpload } from '@/api/mine.js';
export default {
	data() {
		return {
			consultlist: [],
			queryParams: {
				pageNo: 1,
				pageSize: 10
			},
			show: false,
			fileList2: [],
			loading: false,
			loadstatus: 'loading',
			formdata: {
				title: '',
				info: '',
				contactNickname: '',
				contactMobile: '',
				pics: []
			},
			errtext: ''
		};
	},
	onLoad() {
		this.get_zjzx();
	},
	methods: {
		click_listone(url) {
			uni.previewImage({
				urls: [url]
			});
		},
		call_phone(number) {
			uni.makePhoneCall({
				phoneNumber: number
			});
		},
		getMediaType(url) {
			const imageExtensions = /\.(jpg|jpeg|png|gif)$/i;
			const videoExtensions = /\.(mp4|webm)$/i;
			if (imageExtensions.test(url)) {
				return 'image';
			} else if (videoExtensions.test(url)) {
				return 'video';
			} else {
				return 'unknown';
			}
		},
		click_idea() {
			if (!this.formdata.title || !this.formdata.info || !this.formdata.contactNickname || !this.formdata.contactMobile) {
				uni.$u.toast('请填写内容');
				// this.errtext = '请填写内容';
				return;
			}
			if (this.fileList2.length > 0) {
				for (var i = 0; i < this.fileList2.length; i++) {
					this.formdata.pics.push(this.fileList2[i].url);
				}
			}
			sspcreate(this.formdata).then((res) => {
				if (res.code == '0') {
					uni.$u.toast('发布成功，请等待审核');
					this.queryParams.pageNo = 1;
					this.consultlist = [];
					this.get_zjzx();
					this.close();
				} else {
					uni.$u.toast(res.msg);
					// this.errtext = res.msg;
				}
			});
		},
		close() {
			this.show = false;
			this.fileList2 = [];
			this.errtext = '';
			this.formdata = {
				title: '',
				info: '',
				contactNickname: '',
				contactMobile: '',
				pics: []
			};
		},
		get_zjzx() {
			ssppage(this.queryParams).then((res) => {
				this.loading = true;
				if (res.data.list.length == 0 && this.queryParams.pageNo > 1) {
					this.loadstatus = 'nomore';
					return;
				}
				if (this.queryParams.pageNo == 1 && res.data.list.length < 10) {
					this.loadstatus = 'nomore';
				}
				this.consultlist = this.consultlist.concat(res.data.list);
				this.queryParams.pageNo = res.data.list.length == 0 ? this.queryParams.pageNo : (this.queryParams.pageNo += 1);
			});
		},
		// 删除图片
		deletePic(event) {
			this[`fileList${event.name}`].splice(event.index, 1);
		},
		// 新增图片
		async afterRead(event) {
			// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
			let lists = [].concat(event.file);
			let fileListLen = this[`fileList${event.name}`].length;
			lists.map((item) => {
				this[`fileList${event.name}`].push({
					...item,
					status: 'uploading',
					message: '上传中'
				});
			});
			for (let i = 0; i < lists.length; i++) {
				const result = await this.uploadFilePromise(lists[i].url);
				let item = this[`fileList${event.name}`][fileListLen];
				this[`fileList${event.name}`].splice(
					fileListLen,
					1,
					Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					})
				);
				fileListLen++;
			}
		},
		uploadFilePromise(url) {
			return new Promise((resolve, reject) => {
				let a = commonUpload({ url: url }).then((res) => {
					if (res.code == '0') {
						setTimeout(() => {
							resolve(res.data);
						}, 50);
					} else {
						uni.$u.toast('上传失败');
					}
				});
			});
		}
	},
	onPullDownRefresh() {
		this.queryParams.pageNo = 1;
		this.consultlist = [];
		this.get_zjzx();
	},
	onReachBottom() {
		this.get_zjzx();
		this.loadstatus = 'loading';
	}
};
</script>

<style lang="scss">
.list {
	padding: 24rpx;
	.list-one {
		padding: 20rpx;
		background: #ffffff;
		box-shadow: 0px 8rpx 40rpx 0px rgba(0, 0, 0, 0.1);
		border-radius: 12rpx;
		margin-bottom: 20rpx;
		.left {
			padding: 20rpx 0;
		}
		.right {
			flex: auto;
			.tit {
				font-size: 28rpx;
				font-weight: bold;
				line-height: 36rpx;
				min-height: 72rpx;
			}
			.bottm {
				margin-top: 12rpx;
				font-size: 28rpx;
				font-weight: bold;
				line-height: 36rpx;
			}
			.red {
				color: red;
			}
			.time {
				margin-top: 12rpx;
				color: #666666;
				font-size: 24rpx;
				line-height: 36rpx;
				text-align: right;
			}
		}
	}
}
.btn {
	position: fixed;
	top: 50%;
	z-index: 9;
	z-index: 9;
	z-index: 9;
	right: 12rpx;
	width: 182rpx;
	height: 206rpx;
	text-align: center;
	background: url('../../../static/index/btn.png') no-repeat;
	background-size: 100% 100%;
	color: #3c6d09;
	font-size: 24rpx;
	.topval {
		text-indent: 1em;
		margin-top: 120rpx;
		line-height: 36rpx;
	}
	.bottmval {
		text-indent: 1em;
		line-height: 36rpx;
	}
}
.popup-tit {
	line-height: 44rpx;
	padding-top: 48rpx;
	text-align: center;
}
.popup-cont {
	padding: 24rpx 48rpx 48rpx;
	max-height: 800rpx;
	overflow: auto;
	.tit {
		text-align: left;
		font-size: 26rpx;
		line-height: 44rpx;
		font-weight: bold;
		margin-top: 12rpx;
		margin-bottom: 12rpx;
	}
	.text {
		margin: 0;
		border: 2rpx solid #d8d8d8;
		border-radius: 12rpx;
	}
	.btns {
		margin: 64rpx;
		height: 88rpx;
		background: #8bb950;
		border-radius: 20rpx;
		color: #fff;
		text-align: center;
		line-height: 88rpx;
	}
}
</style>
